package com.barbeque.mall.controller.wx;

import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.advertise.MarketAd;
import com.barbeque.mall.bean.advertise.MarketTopic;
import com.barbeque.mall.bean.brand.MarketBrand;
import com.barbeque.mall.bean.category.MarketCategory;
import com.barbeque.mall.bean.goods.MarketGoods;
import com.barbeque.mall.bean.wx.coupon.WxMarketCoupon;
import com.barbeque.mall.bean.wx.goods.EnterFloorGoodsList;
import com.barbeque.mall.bean.wx.goods.FloorGoodsList;
import com.barbeque.mall.bean.wx.goods.WxGoodsIndexVO;
import com.barbeque.mall.service.wx.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 苏东宇
 * @since 2022/07/20 23:02
 */
@RestController
@RequestMapping("wx/home")
public class WxHomeController {

    private static final Integer FLOOR_CATEGORY_NUM = 3;

    @Autowired
    WxGoodsService wxGoodsService;
    @Autowired
    WxCouponService wxCouponService;
    @Autowired
    WxCatalogService wxCatalogService;
    @Autowired
    WxAdService wxAdService;
    @Autowired
    WxBrandService wxBrandService;
    @Autowired
    WxTopicService wxTopicService;

    @RequestMapping("index")
    public BaseRespVo getGoodsIndex() {
        // 周一周四·新品首发(limit:6)
        List<MarketGoods> newGoodsList = wxGoodsService.getNewGoodsList();
        // 优惠券(limit:#)
        List<WxMarketCoupon> couponList = wxCouponService.getCouponList();
        // 一级类目(limit:10)
        List<MarketCategory> channel = wxCatalogService.getCatalogByPid(0).subList(0, 10);
        // banner(limit:20)
        List<MarketAd> allAds = wxAdService.getAllAds();
        // 品牌制造商直供(limit:3)
        List<MarketBrand> allBrandList = wxBrandService.getAllBrandList();
        // 人气推荐(limit:6)
        List<MarketGoods> hotGoodsList = wxGoodsService.getHotGoodsList();
        // 专题精选(limit:4)
        List<MarketTopic> topicList = wxTopicService.getTopicList();
        // floorGoodsList
        List<FloorGoodsList> floorGoodsLists = new ArrayList<>();
        // 将查询到的一级类目channel中：name 和 id 信息赋值
        for (int i = 0; i < FLOOR_CATEGORY_NUM; i++) {
            FloorGoodsList floorGoods = new FloorGoodsList();
            BeanUtils.copyProperties(channel.get(i), floorGoods);
            floorGoodsLists.add(floorGoods);
        }
        for (FloorGoodsList floorGoods : floorGoodsLists) {
            // 根据pid获取其子类目ids
            Integer[] ids = wxCatalogService.getIdsByPid(floorGoods.getId());
            List<MarketGoods> marketGoodsList = new ArrayList<>();
            for (Integer id : ids) {
                List<MarketGoods> goodsByCategoryId = wxGoodsService.getGoodsByCategoryId(id);
                marketGoodsList.addAll(goodsByCategoryId);
                if (marketGoodsList.size() > 6) {
                    floorGoods.setGoodsList(marketGoodsList.subList(0,6));
                    break;
                }
            }
        }


        WxGoodsIndexVO wxGoodsIndexVO = new WxGoodsIndexVO();
        wxGoodsIndexVO.setNewGoodsList(newGoodsList);
        wxGoodsIndexVO.setCouponList(couponList);
        wxGoodsIndexVO.setChannel(channel);
        wxGoodsIndexVO.setBanner(allAds);
        wxGoodsIndexVO.setBrandList(allBrandList);
        wxGoodsIndexVO.setHotGoodsList(hotGoodsList);
        wxGoodsIndexVO.setTopicList(topicList);
        wxGoodsIndexVO.setFloorGoodsList(floorGoodsLists);
        return BaseRespVo.ok(wxGoodsIndexVO);

    }

}
